package com.teamcenter.rac.massupdate.ui;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.teamcenter.rac.aif.binding.SWTObservablesFactory;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.massupdate.converters.Source2LabelConverter;
import com.teamcenter.rac.massupdate.model.SubmitToWorkflowDialogModel;
import com.teamcenter.rac.massupdate.model.UpdateJobModel;
import com.teamcenter.rac.massupdate.model.UpdateResult;
import com.teamcenter.rac.massupdate.ui.grid.GridColumn;
import com.teamcenter.rac.massupdate.ui.grid.GridConfigurator;
import com.teamcenter.rac.massupdate.ui.grid.GridModel;
import com.teamcenter.rac.massupdate.ui.grid.IEditorConfig;
import com.teamcenter.rac.massupdate.ui.grid.UpdateResultCellImagePainter;
import com.teamcenter.rac.massupdate.utils.MassUpdateHelper;
import com.teamcenter.rac.massupdate.utils.MassUpdateTableHelper;
import com.teamcenter.rac.viewer.view.ViewerHelper;

import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import net.sourceforge.nattable.NatTable;
import net.sourceforge.nattable.config.AbstractRegistryConfiguration;
import net.sourceforge.nattable.config.CellConfigAttributes;
import net.sourceforge.nattable.config.IConfigRegistry;
import net.sourceforge.nattable.config.IEditableRule;
import net.sourceforge.nattable.data.IRowIdAccessor;
import net.sourceforge.nattable.data.convert.DefaultBooleanDisplayConverter;
import net.sourceforge.nattable.data.validate.IDataValidator;
import net.sourceforge.nattable.edit.action.ToggleCheckBoxColumnAction;
import net.sourceforge.nattable.edit.editor.CheckBoxCellEditor;
import net.sourceforge.nattable.edit.editor.ICellEditor;
import net.sourceforge.nattable.layer.DataLayer;
import net.sourceforge.nattable.painter.cell.CheckBoxPainter;
import net.sourceforge.nattable.painter.cell.ColumnHeaderCheckBoxPainter;
import net.sourceforge.nattable.painter.cell.TextPainter;
import net.sourceforge.nattable.painter.cell.decorator.BeveledBorderDecorator;
import net.sourceforge.nattable.painter.cell.decorator.CellPainterDecorator;
import net.sourceforge.nattable.style.CellStyleAttributes;
import net.sourceforge.nattable.style.HorizontalAlignmentEnum;
import net.sourceforge.nattable.style.Style;
import net.sourceforge.nattable.ui.binding.UiBindingRegistry;
import net.sourceforge.nattable.ui.matcher.CellPainterMouseEventMatcher;
import net.sourceforge.nattable.ui.util.CellEdgeEnum;
@SuppressWarnings({"rawtypes","unchecked"})
public class SubmitResultsToWorkflowDialog
  extends AbstractMassUpdateDialog
{
  private Label m_changeObjectLabel;
  private Text m_changeObjectText;
  private Button m_submitButton;
  private Label m_replacementPartLabel;
  private Text m_replacementPartText;
  private Text m_targetPartText;
  private UpdateJobModel m_updateJobModel;
  private final SubmitToWorkflowDialogModel m_dialogModel;
  private GridConfigurator m_tableConfigurator;
  private NatTable m_resultsTable;
  public static final String[] PROPERTY_NAMES = { "selected", "object_string", "resultStatus", "resultComment", "release_status_list", "owning_user", "owning_group" };
  
  public SubmitResultsToWorkflowDialog(Shell paramShell, UpdateJobModel paramUpdateJobModel, SubmitToWorkflowDialogModel paramSubmitToWorkflowDialogModel)
  {
    super(paramShell);
    setShellStyle(34032);
    this.m_updateJobModel = paramUpdateJobModel;
    this.m_dialogModel = paramSubmitToWorkflowDialogModel;
  }
  
  protected void createButtonsForButtonBar(Composite paramComposite)
  {
    this.m_submitButton = createButton(paramComposite, 1090, IMassUpdateDialogConstants.SUBMIT_LABEL, false);
    this.m_submitButton.setToolTipText(Messages.SubmitToWorkFlowDialog_submitButton_toolTipText);
    this.m_submitButton.addSelectionListener(new SelectionAdapter()
    {
      public void widgetSelected(SelectionEvent paramAnonymousSelectionEvent)
      {
        SubmitResultsToWorkflowDialog.this.submitToWorkflow();
      }
    });
    this.m_submitButton.setEnabled(false);
    Button localButton = createButton(paramComposite, 12, IDialogConstants.CLOSE_LABEL, true);
    localButton.addSelectionListener(new SelectionAdapter()
    {
      public void widgetSelected(SelectionEvent paramAnonymousSelectionEvent)
      {
        SubmitResultsToWorkflowDialog.this.close();
      }
    });
    localButton.setFocus();
    this.m_bindingContext = initDataBindings();
    setupAggregateValidationStatus();
    initCommonDataBindings(this.m_bindingContext);
  }
  
  private void submitToWorkflow()
  {
    List localList = this.m_dialogModel.getSelectedComponents();
    MassUpdateHelper.submitTargetsToWorkflow((InterfaceAIFComponent[])localList.toArray(new TCComponent[localList.size()]));
  }
  
  protected Control createContents(Composite paramComposite)
  {
    Control localControl = super.createContents(paramComposite);
    setTitle(Messages.SubmitToWorkflowDialog_Title);
    getShell().setText(Messages.SubmitToWorkflowDialog_Title);
    setMessage("");
    return localControl;
  }
  
  protected Control createDialogArea(Composite paramComposite)
  {
    Composite localComposite1 = (Composite)super.createDialogArea(paramComposite);
    localComposite1.setLayout(new GridLayout(1, false));
    Composite localComposite2 = new Composite(localComposite1, 0);
    localComposite2.setLayoutData(new GridData(4, 4, true, true, 1, 1));
    localComposite2.setLayout(new GridLayout(1, false));
    contributeHeaderGroup(localComposite2);
    contributeResultsTableGroup(localComposite2);
    return localComposite1;
  }
  
  protected void initCommonDataBindings(DataBindingContext paramDataBindingContext) {}
  
  protected DataBindingContext initDataBindings()
  {
    DataBindingContext localDataBindingContext = new DataBindingContext();
    ISWTObservableValue localISWTObservableValue1 = SWTObservables.observeText(this.m_targetPartText, 24);
    IObservableValue localIObservableValue1 = BeansObservables.observeValue(this.m_updateJobModel, "targetPart");
    UpdateValueStrategy localUpdateValueStrategy1 = new UpdateValueStrategy();
    localUpdateValueStrategy1.setConverter(new Source2LabelConverter());
    localDataBindingContext.bindValue(localISWTObservableValue1, localIObservableValue1, new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), localUpdateValueStrategy1);
    ISWTObservableValue localISWTObservableValue2 = SWTObservables.observeText(this.m_replacementPartText, 24);
    IObservableValue localIObservableValue2 = BeansObservables.observeValue(this.m_updateJobModel, "replacementPart");
    UpdateValueStrategy localUpdateValueStrategy2 = new UpdateValueStrategy();
    localUpdateValueStrategy2.setConverter(new Source2LabelConverter());
    localDataBindingContext.bindValue(localISWTObservableValue2, localIObservableValue2, new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), localUpdateValueStrategy2);
    ISWTObservableValue localISWTObservableValue3 = SWTObservables.observeText(this.m_changeObjectText, 24);
    IObservableValue localIObservableValue3 = BeansObservables.observeValue(this.m_updateJobModel, "changeObject");
    UpdateValueStrategy localUpdateValueStrategy3 = new UpdateValueStrategy();
    localUpdateValueStrategy3.setConverter(new Source2LabelConverter());
    localDataBindingContext.bindValue(localISWTObservableValue3, localIObservableValue3, new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), localUpdateValueStrategy3);
    ISWTObservableValue localISWTObservableValue4 = SWTObservables.observeVisible(this.m_changeObjectLabel);
    IObservableValue localIObservableValue4 = BeansObservables.observeValue(this.m_updateJobModel, "changeSupported");
    localDataBindingContext.bindValue(localISWTObservableValue4, localIObservableValue4, new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
    ISWTObservableValue localISWTObservableValue5 = SWTObservables.observeVisible(this.m_changeObjectText);
    localDataBindingContext.bindValue(localISWTObservableValue5, localIObservableValue4, new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
    IObservableValue localIObservableValue5 = SWTObservablesFactory.getInstance().observeEnabled(this.m_submitButton);
    IObservableValue localIObservableValue6 = BeansObservables.observeValue(this.m_dialogModel, "somethingSelected");
    localDataBindingContext.bindValue(localIObservableValue5, localIObservableValue6, new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
    return localDataBindingContext;
  }
  
  private GridColumn[] contributeResultsTableColumns()
  {
    int[] arrayOfInt = { 75, 160, 120, 110, 110, 110, 90 };
    int i = PROPERTY_NAMES.length;
    GridColumn[] arrayOfGridColumn = new GridColumn[i];
    String[] arrayOfString1 = ViewerHelper.getTextValues(new String[] { "release_status_list", "owning_user", "owning_group" });
    String[] arrayOfString2 = { "", Messages.ResultsDialog_Col_ImpactedPart, Messages.ResultsDialog_Col_Status, Messages.ResultsDialog_Col_Comments, arrayOfString1[0], arrayOfString1[1], arrayOfString1[2] };
    for (int j = 0; j < i; j++)
    {
      GridColumn localGridColumn = new GridColumn(j, PROPERTY_NAMES[j], arrayOfString2[j]);
      localGridColumn.setWidth(Integer.valueOf(arrayOfInt[j]));
      arrayOfGridColumn[j] = localGridColumn;
    }
    int j = MassUpdateTableHelper.getIndexByValue(PROPERTY_NAMES, "object_string");
    arrayOfGridColumn[j].setCellPainter(new CellPainterDecorator(new TextPainter(), CellEdgeEnum.LEFT, new UpdateResultCellImagePainter("object_string", "")));
    int k = MassUpdateTableHelper.getIndexByValue(PROPERTY_NAMES, "selected");
    arrayOfGridColumn[k].setEditable(true);
    arrayOfGridColumn[k].setEditorConfig(new IEditorConfig()
    {
      public IEditorConfig.EditorType getEditorType()
      {
        return IEditorConfig.EditorType.CHECKBOX;
      }
      
      public IDataValidator getValidator()
      {
        return null;
      }
      
      public IEditableRule getEditableRule()
      {
        return IEditableRule.ALWAYS_EDITABLE;
      }
      
      public ICellEditor getCellEditor()
      {
        return new CheckBoxCellEditor();
      }
    });
    arrayOfGridColumn[k].setDisplayConverter(new DefaultBooleanDisplayConverter());
    arrayOfGridColumn[k].setCellPainter(new CheckBoxPainter());
    Style localStyle = new Style();
    localStyle.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.CENTER);
    arrayOfGridColumn[k].setColumnStyle(localStyle);
    arrayOfGridColumn[k].setColumnHeaderStyle(localStyle);
    int m = MassUpdateTableHelper.getIndexByValue(PROPERTY_NAMES, "owning_user");
    arrayOfGridColumn[m].setCellPainter(new CellPainterDecorator(new TextPainter(), CellEdgeEnum.LEFT, new UpdateResultCellImagePainter("owning_user", "object_string", "")));
    int n = MassUpdateTableHelper.getIndexByValue(PROPERTY_NAMES, "owning_group");
    arrayOfGridColumn[n].setCellPainter(new CellPainterDecorator(new TextPainter(), CellEdgeEnum.LEFT, new UpdateResultCellImagePainter("owning_group", "object_string", "")));
    int i1 = MassUpdateTableHelper.getIndexByValue(PROPERTY_NAMES, "release_status_list");
    arrayOfGridColumn[i1].setCellPainter(new CellPainterDecorator(new TextPainter(), CellEdgeEnum.RIGHT, new UpdateResultCellImagePainter("release_status_list", "")));
    return arrayOfGridColumn;
  }
  
  private void contributeHeaderGroup(Composite paramComposite)
  {
    GridLayout localGridLayout1 = new GridLayout(1, false);
    localGridLayout1.verticalSpacing = 0;
    localGridLayout1.marginWidth = 0;
    localGridLayout1.marginHeight = 0;
    localGridLayout1.horizontalSpacing = 0;
    Group localGroup = new Group(paramComposite, 0);
    localGroup.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
    localGroup.setText(Messages.ResultsDialog_Grp_OperationalData);
    localGroup.setLayout(localGridLayout1);
    GridLayout localGridLayout2 = new GridLayout(2, false);
    localGridLayout2.marginLeft = 10;
    Composite localComposite = new Composite(localGroup, 0);
    localComposite.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
    localComposite.setLayout(localGridLayout2);
    Label localLabel = new Label(localComposite, 0);
    FontData localFontData1 = localLabel.getFont().getFontData()[0];
    localLabel.setFont(JFaceResources.getFontRegistry().getBold(localFontData1.getName()));
    localLabel.setText(Messages.ResultsDialog_TargetPart);
    this.m_targetPartText = new Text(localComposite, 8);
    this.m_targetPartText.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
    this.m_replacementPartLabel = new Label(localComposite, 0);
    GridData localGridData = new GridData(16384, 16777216, false, false, 1, 1);
    localGridData.widthHint = 90;
    this.m_replacementPartLabel.setLayoutData(localGridData);
    FontData localFontData2 = this.m_replacementPartLabel.getFont().getFontData()[0];
    this.m_replacementPartLabel.setFont(JFaceResources.getFontRegistry().getBold(localFontData2.getName()));
    this.m_replacementPartLabel.setText(Messages.ResultsDialog_ReplacementPart);
    this.m_replacementPartText = new Text(localComposite, 8);
    this.m_replacementPartText.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
    this.m_changeObjectLabel = new Label(localComposite, 0);
    FontData localFontData3 = this.m_changeObjectLabel.getFont().getFontData()[0];
    this.m_changeObjectLabel.setFont(JFaceResources.getFontRegistry().getBold(localFontData3.getName()));
    this.m_changeObjectLabel.setText(Messages.ResultsDialog_ChangeObject);
    this.m_changeObjectText = new Text(localComposite, 8);
    this.m_changeObjectText.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
  }
  
  private void contributeResultsTableGroup(Composite paramComposite)
  {
    GridLayout localGridLayout1 = new GridLayout(1, false);
    localGridLayout1.verticalSpacing = 0;
    localGridLayout1.marginWidth = 0;
    localGridLayout1.marginHeight = 0;
    localGridLayout1.horizontalSpacing = 0;
    Group localGroup = new Group(paramComposite, 0);
    localGroup.setLayoutData(new GridData(4, 4, true, true, 1, 1));
    localGroup.setText(Messages.ResultsDialog_Grp_SelectedImpactedParts);
    localGroup.setLayout(localGridLayout1);
    GridLayout localGridLayout2 = new GridLayout(1, false);
    localGridLayout2.verticalSpacing = 0;
    localGridLayout2.marginLeft = 10;
    localGridLayout2.marginWidth = 0;
    localGridLayout2.marginHeight = 0;
    localGridLayout2.horizontalSpacing = 0;
    Composite localComposite = new Composite(localGroup, 0);
    GridData localGridData = new GridData(4, 4, true, true, 1, 1);
    localGridData.verticalIndent = 5;
    localComposite.setLayoutData(localGridData);
    localComposite.setLayout(localGridLayout2);
    GridColumn[] arrayOfGridColumn = contributeResultsTableColumns();
    GridModel localGridModel = new GridModel(arrayOfGridColumn);
    IRowIdAccessor local4 = new IRowIdAccessor<UpdateResult>()
    {
      public Serializable getRowId(UpdateResult paramAnonymousUpdateResult)
      {
        return (Serializable)paramAnonymousUpdateResult.getIdentifier();
      }
    };
    EventList localEventList = GlazedLists.eventList(new ArrayList());
    localEventList.addAll(this.m_dialogModel.getRows());
    this.m_tableConfigurator = new GridConfigurator(localComposite, localGridModel, localEventList, local4);
    this.m_resultsTable = this.m_tableConfigurator.setupLayers();
    this.m_resultsTable.addConfiguration(getSelectAllColHeaderConfiguration());
    this.m_resultsTable.configure();
    this.m_resultsTable.setLayoutData(new GridData(4, 4, true, true, 1, 1));
  }
  
  private AbstractRegistryConfiguration getSelectAllColHeaderConfiguration()
  {
    final DataLayer localDataLayer = this.m_tableConfigurator.getBodyLayerStack().getDataLayer();
    final ColumnHeaderCheckBoxPainter localColumnHeaderCheckBoxPainter = new ColumnHeaderCheckBoxPainter(localDataLayer);
    final BeveledBorderDecorator localBeveledBorderDecorator = new BeveledBorderDecorator(localColumnHeaderCheckBoxPainter);
    return new AbstractRegistryConfiguration()
    {
      public void configureRegistry(IConfigRegistry paramAnonymousIConfigRegistry)
      {
        String str = "colHdrLbl_" + MassUpdateTableHelper.getIndexByValue(SubmitResultsToWorkflowDialog.PROPERTY_NAMES, "selected");
        paramAnonymousIConfigRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, localBeveledBorderDecorator, "NORMAL", str);
      }
      
      public void configureUiBindings(UiBindingRegistry paramAnonymousUiBindingRegistry)
      {
        paramAnonymousUiBindingRegistry.registerFirstSingleClickBinding(new CellPainterMouseEventMatcher("COLUMN_HEADER", 1, localColumnHeaderCheckBoxPainter), new ToggleCheckBoxColumnAction(localColumnHeaderCheckBoxPainter, localDataLayer));
      }
    };
  }
}
